﻿using System;
using CardPlay.DemoPack.Games.KlondikeVariants;
using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Actions;

namespace CardPlay.DemoPack.Games.BakersDozenVariants
{
    [Game("E0BEFD48-8977-41CD-91F8-5567ADC36D3F", LevelOfDifficulty.Medium, LevelOfSkill.MostlySkill, DurationOfPlay.Within5To10Minutes, Family = FamilyName)]
    public class GoodMeasure : Canister
    {
        public new const string FamilyName = "Baker's Dozen";

        public GoodMeasure()
        {
            TotalTableaus = 10;
            TableauBuildSequence = CardFaceSequence.AnySuitDescendingRank;

            InitialDealCompleted += OnDealCompleted;
        }

        protected override void PrepareDealScript(DealScript script)
        {
            script.Deal("face(hearts ace) up").To(Foundation1);
            script.Deal("face(spades ace) up").To(Foundation2);
            script.DealToEach(NamesOfTableaus, "5 up");
        }

        protected override bool AllowedToPickupAtTableau(RemovePileAction action)
        {
            return action.IsActingOnFaceUpTopCard();
        }

        protected override bool AllowedToBuildEmptyTableau(MovePileAction action)
        {
            return false;
        }

        protected override bool AllowedToBuildNonEmptyTableau(MovePileAction action)
        {
            return action.TargetPile.TopCard.Rank != FoundationBaseRank && base.AllowedToBuildNonEmptyTableau(action);
        }

        private void OnDealCompleted(object sender, EventArgs e)
        {
            foreach (var pile in PilesNamed(AnyTableau))
                pile.Sort((a, b) => (a.Rank != Rank.King && b.Rank == Rank.King) ? 1 : (a.Rank == Rank.King && b.Rank != Rank.King ? -1 : 0));
        }
    }
}